package com.thz.websqlit.common.util;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static Logger logger = LogManager.getLogger("DateUtil");
    private final static SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat FORMAT_SAME_MINUTE = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private final static SimpleDateFormat FORMAT_SAME_YEAR = new SimpleDateFormat("yyyy");
    private final static SimpleDateFormat OUT_FORMATE_SAME_YEAR = new SimpleDateFormat("MM月dd日 HH时mm分");
    private final static SimpleDateFormat FORMAT_SAME_DATE = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat OUT_SAME_DATE = new SimpleDateFormat("HH时mm分");
    private final static SimpleDateFormat OUT_SAME_MINUTE = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
    private final static String JUST_DATE = "刚刚";
    public static String formateDate(String src){
        //logger.info("srcDate:" + src);
        Date now = Calendar.getInstance().getTime();
        String nowStr = FORMAT_SAME_MINUTE.format(now);
        String outStrDate = "";

        try {
            Date inData = FORMAT_DATE.parse(src);

            if(nowStr.equals(FORMAT_SAME_MINUTE.format(inData))){ //同一分钟，为刚刚
                outStrDate = JUST_DATE;
            }else if(FORMAT_SAME_DATE.format(now).equals(FORMAT_SAME_DATE.format(inData))){ //同一天
                outStrDate = OUT_SAME_DATE.format(inData);
            }else if(FORMAT_SAME_YEAR.format(now).equals(FORMAT_SAME_YEAR.format(inData))){ //同一年
                outStrDate =OUT_FORMATE_SAME_YEAR.format(inData);
            }
            else {
                outStrDate = OUT_SAME_MINUTE.format(inData);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return outStrDate;

    }
}
